package com.base.utils;

import android.content.Context;
import android.content.SharedPreferences;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.util.ArrayList;
import java.util.List;

/**
 * 自定义sharedpreferences
 * Created by Android on 2017/3/7 0007.
 */

public class MySharedPreferencesMgr {
    private static Context context;
    private static SharedPreferences sPrefs ;

    private MySharedPreferencesMgr(Context context,String fileName)
    {
        this.context=context;
        sPrefs= context.getSharedPreferences(
                fileName, Context.MODE_APPEND );
    }

    public static void init(Context context,String fileName)
    {
        new MySharedPreferencesMgr(context,fileName);
    }
    public   static String fileName ;

    public static int getInt(String key,int defaultValue)
    {
        return sPrefs.getInt(key, defaultValue);
    }
    public static void setInt(String key,int value) {
        sPrefs.edit().putInt(key, value).commit();
    }
    public static boolean getBoolean(String key,boolean defaultValue)
    {
        return sPrefs.getBoolean(key, defaultValue);
    }
    public static void setBoolean(String key,boolean value) {
        sPrefs.edit().putBoolean(key, value).commit();
    }

    public static String getString(String key,String defaultValue)
    {
        if(sPrefs ==null)
            return null;
        return sPrefs.getString(key, defaultValue);
    }

    public static void setString(String key,String value) {
        if(sPrefs ==null)
            return ;
        sPrefs.edit().putString(key, value).commit();
    }

    public static void setLong(String key,long value) {
        if(sPrefs ==null)
            return ;
        sPrefs.edit().putLong(key, value).commit();
    }

    public static long getLong(String key,long value){
        return sPrefs.getLong(key,value);
    }

    public static void setList(String key, List<String> value) {
        if (sPrefs == null) {
            return;
        }
        if (value != null) {
            Gson gson = new Gson();
            String strJson = gson.toJson(value);
            sPrefs.edit().putString(key, strJson).commit();
        }
    }

    public static List<String> getList(String key) {
        List<String> datas;
        String strJson = sPrefs.getString(key, "");
        if (StringUtil.isEmpty(strJson)) {
            return null;
        }
        Gson gson = new Gson();
        datas = gson.fromJson(strJson, new TypeToken<List<String>>(){}.getType());
        return datas;
    }
}
